import Vue from 'vue'
import Vuex from 'vuex';
import createPersistedState from "vuex-persistedstate";
import logger from "vuex/dist/logger";
import request from "../utils/request";
import "../utils/api";
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    userData: []
  },
  mutations: {
    getuserdata(state, payload) {
      state.userData = payload;
    },
    // 用户信息管理 删除信息
    userManagementDeleteitem(state, payload) {
      const newUserData = [...state.userData]
      let newListData = []
      newUserData.forEach((item, index) => {
        if (item.id != payload) {
          newListData.push(item)
        }
      })
      state.userData = newListData
    },
    // 点击 编辑保存按钮
    userManagementRdit(state, payload) {
      const newUserData = [...state.userData];
      newUserData.forEach(item => {
        if (item.id == payload.id) {
          Object.assign(item, payload)
        }
      })
      state.userData = newUserData
    },
    // 点击添加内容
    userManagementAdd(state,payload){
      // console.log(payload);
      payload.id = new Date() * 1;
      const newData = state.userData;
      newData.unshift(payload);
      state.userData = newData
    }
  },
  actions: {
    getUserData(context) {
      request.get("/api/list").then(res => {
        context.commit("getuserdata", res.data.userData)
      })
    }
  },
  modules: {
  },
  plugins: [createPersistedState(), logger()],
})
